package com.lin.filegraph.utils.parser.simpleast;

import com.lin.filegraph.utils.parser.astutil.ASTFileASTRequestor;
import com.lin.filegraph.utils.parser.astvisitor.TypeContentVisitor;
import com.lin.filegraph.utils.parser.astvisitor.TypeVisitor;
import com.lin.filegraph.utils.parser.astvisitor.FileVisitor;
import com.lin.filegraph.utils.parser.astvisitor.ImportVisitor;
import com.lin.filegraph.utils.parser.astvisitor.MethodInvocationVisitor;
import com.lin.filegraph.utils.parser.astvisitor.MethodQuoteVisitor;
import com.lin.filegraph.utils.parser.node.*;

import java.util.List;

/**
 * @description:
 * @author: linhuaixu
 * @time: 2023/5/18 11:10
 */

public class SimpleASTCreator {

    private String projectName;
    private String versionNo;
    private String projectPath;
    private ASTFileASTRequestor astFileASTRequestor;

    public SimpleASTCreator(String projectName, String versionNo, String projectPath,
                            ASTFileASTRequestor astFileASTRequestor) {
        this.projectName = projectName;
        this.versionNo = versionNo;
        this.projectPath = projectPath;
        this.astFileASTRequestor = astFileASTRequestor;
    }

    public JProjectNode createSimpleAST() {
        JProjectNode root = new JProjectNode(projectName, versionNo, projectPath, astFileASTRequestor);

        FileVisitor fileVisitor = new FileVisitor(root);
        fileVisitor.traverse();

        int LOC = 0;
        int count = 0;
        for (JDirectoryNode directoryNode : root.getAllDirectories()) {
            for (JPackageNode packageNode : directoryNode.getAllPackages()) {
                for (JFileNode fileNode : packageNode.getAllFiles()) {

                    ImportVisitor importVisitor = new ImportVisitor(fileNode, root);
                    importVisitor.traverse();

                    TypeVisitor typeVisitor = new TypeVisitor(fileNode);
                    typeVisitor.traverse();
                    for (JTypeNode typeNode : fileNode.getAllTypes()) {
                        TypeContentVisitor typeContentVisitor = new TypeContentVisitor(typeNode);
                        typeContentVisitor.visitTypeContent();
                    }
                    LOC +=  fileNode.getLOC();
                    count ++;
                }
            }
        }
        List<JMethodNode> methodNodes = SimpleASTHelper.findAllMethodsInSubTree(root);
        for (JMethodNode methodNode : methodNodes) {

            MethodInvocationVisitor methodInvocationVisitor = new MethodInvocationVisitor(methodNode);
            methodInvocationVisitor.traverse();
            MethodQuoteVisitor quoteVisitor = new MethodQuoteVisitor(methodNode);
            // 引用依赖提取
            quoteVisitor.traverse();
            methodNode.setListOfQuoteTypes(quoteVisitor.getListOfQuoteTypes());
        }
        return root;
    }

    public JProjectNode createNullSimpleAST() {
        JProjectNode root = new JProjectNode(projectName, versionNo, projectPath, astFileASTRequestor);

        FileVisitor fileVisitor = new FileVisitor(root);
        fileVisitor.traverse();
        return root;
    }
}

